//
//  MTInfoVerificationPortraitCell.swift
//  MT
//
//  Created by M on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MTInfoVerificationPortraitCell: BaseMTInfoVerificationCell {
    
    var asset: MTAsset? {
        didSet {
            requestPhoto(with: asset)
        }
    }
    
    var loadURL: String? {
        didSet {
            guard let url = loadURL else { return }
            self.cameraImgView.kf.setImage(with: URL(string: url), placeholder: UIImage(named: "icon_camera"))
            self.cameraImgView.layer.cornerRadius = 32
            self.cameraImgView.clipsToBounds = true
        }
    }
    
    /// 相机
    lazy var cameraImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "icon_camera")
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        let t = UITapGestureRecognizer(target: self, action: #selector(portraitCell(didActiveGestureRecognizer:)))
        imgView.addGestureRecognizer(t)
        return imgView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MTInfoVerificationPortraitCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        titleLabel.textColor = UIColor(hex: 0x1d1a1a)
        titleLabel.font = PingFangSCSemiboldFont(14)
        underline.backgroundColor = UIColor(hex: 0xe7e7e7)
    }
    
    private func initializeUI() {
        contentView.addSubview(cameraImgView)
        cameraImgView.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentView)
            make.right.equalTo(contentView).offset(-20)
            make.width.height.equalTo(64)
        }
        
        titleLabel.snp.remakeConstraints { (make) in
            make.left.equalTo(contentView).offset(16)
            make.centerY.equalTo(contentView)
            make.height.equalTo(22)
            make.width.equalTo(200)
        }
        
        underline.snp.remakeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.bottom.equalTo(contentView)
            make.height.equalTo(0.5)
        }
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension MTInfoVerificationPortraitCell {
    
    private func portraitCell(didActiveGestureRecognizer gestureRecognizer: UITapGestureRecognizer) {
        NotificationCenter.default.post(name: GlobalNotificationName.presentPhotoLibrary.name, object: nil, userInfo: ["type":type?.rawValue ?? -1])
    }
    
}

// MARK: ------------------ 图片请求 ----------------------
extension MTInfoVerificationPortraitCell {
    
    private func requestPhoto(with asset: MTAsset?) {
        guard let asset = asset else { return }
        MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth / 4, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
            guard let weakself = self else { return }
            /// 设置图片
            weakself.cameraImgView.image = image
        }
    }
}
